package main

import (
	"fmt"
	"go_code/project01_basic/chapter15_qq/message"
	"go_code/project01_basic/chapter15_qq/server/model"
	"go_code/project01_basic/chapter15_qq/server/processes"
	"net"
	"time"
)

func init() {
	initPool("localhost:6379", 16, 0, 300*time.Second)
	model.MyUserDao = model.NewUserDao(pool)
}

func main() {
	fmt.Println("服务器在8889端口监听...")
	listen, err := net.Listen("tcp", "10.251.50.46:8889")
	if err != nil {
		fmt.Println("net listen err=", err)
		return
	}
	defer listen.Close()
	//获取全部用户
	res := model.MyUserDao.GetAllUsers()
	for _, id := range res {
		up := &processes.UserProcess{
			UserId:     id,
			UserStatus: message.UserOffline,
		}
		processes.AllUsers.Users[id] = up
	}
	//和用户通信
	for {
		fmt.Println("等待客户端来连接服务器...")
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println("listen.Accept err=", err)
		}
		go process(conn)
	}
}

func process(conn net.Conn) {
	defer conn.Close()

	processer := &Processor{
		Conn: conn,
	}

	err := processer.processing()
	if err != nil {
		fmt.Println(err)
		return
	}
}
